package com.jincp.image.load.glide_loder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.rastermill.FrameSequenceDrawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;
import com.jincp.image.load.gif.GifSoftwareLayerSetter;

/**
 * Created by admin on 2017/3/23.
 */

public class GlideImageLoader extends ImageLoader {

    private static volatile Context context;

    @Override
    public void init(Context context) {
        GlideImageLoader.context = context.getApplicationContext();
    }

    @Override
    public <T> void displayImage(T t, ImageView imageView) {
        displayImage(t, imageView, new ImageLoaderOptions.Builder().build());
    }

    @Override
    public <T> void displayImage(T t, final ImageView imageView,  @NonNull final ImageLoaderOptions options) {
        if (t == null || imageView == null) return;
        //装配附加参数
        RequestOptions requestOptions = loadOptions(options);
        //装配基本的参数
        GlideApp.with(context).asDrawable().load(t).apply(requestOptions).listener(new RequestListener<Drawable>() {

            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                if (options.getImageLoadListener() != null){
                    options.getImageLoadListener().onLoadFailure();
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                if (options.getImageLoadListener() != null){
                    options.getImageLoadListener().onLoadSucceed(resource);
                }
                return false;
            }
        }).into(imageView);
    }

    @Override
    public <T> void displayBitmap(T t, ImageView imageView) {
        displayBitmap(t, imageView, new ImageLoaderOptions.Builder().build());
    }

    @Override
    public <T> void displayBitmap(T t, ImageView imageView,  @NonNull final ImageLoaderOptions options) {
        if (t == null || imageView == null) return;
        //装配附加参数
        RequestOptions requestOptions = loadOptions(options);
        //装配基本的参数
        GlideApp.with(context).asBitmap().load(t).apply(requestOptions).listener(new RequestListener<Bitmap>() {

            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                if (options.getImageLoadListener() != null){
                    options.getImageLoadListener().onLoadFailure();
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                if (options.getImageLoadListener() != null){
                    options.getImageLoadListener().onLoadSucceed(resource);
                }
                return false;
            }
        }).into(imageView);
    }

    @Override
    public <T> void displayGif(T t, ImageView imageView) {
        if (t == null || imageView == null) return;
        //采用Giflib来解码GIF
        GlideApp.with(context).asGif2().load(t).listener(new GifSoftwareLayerSetter(FrameSequenceDrawable.LOOP_DEFAULT)).into(imageView);
    }

    @Override
    public void cleanCache() {
        // 主线程操作
        Glide.get(context).clearMemory();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(context).clearDiskCache();
            }
        }).start();
    }

    //这个方法用来装载由外部设置的参数
    @SuppressWarnings("CheckResult")
    private RequestOptions loadOptions(ImageLoaderOptions options) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(options.getDefaultDrawable())
                .error(options.getErrorDrawable())
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        if (options.getReWidth() != 0 && options.getReHeight() != 0) {
            requestOptions.override(options.getReWidth(), options.getReHeight());
        }
        if (options.getImageShape() == ImageLoaderOptions.SHAPE_CIRCLE) {
            requestOptions.transforms(new CenterCrop(), new GlideTransformCircle(options.getBorderSize(), options.getBorderColor()));
        } else if (options.getImageShape() == ImageLoaderOptions.SHAPE_ROUND) {
            requestOptions.transforms(new CenterCrop(), new GlideTransformRound(options.getRoundRadius(), options.getRoundType()));
        }
        return requestOptions;
    }
}
